package com.example.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;


/**
 * @author edehhjn
 * aop 面向切面拦截请求
 */
@Aspect
@Component
public class HttpAspect {

	private final Logger logger = LoggerFactory.getLogger(HttpAspect.class);
	
	/**
	 * 切点，公用方法
	 * 对以下方法进行拦截处理
	 * com.example.controller.GirlController.*
	 * 只是为抽成公共配置
	 */
	@Pointcut("execution(public * com.example.controller.GirlController.*(..))")
	public void comLog(){
		//System.out.println(666666);//这样写是无效的，不会输出
	}
	
	
	/**
	 * 日志打印
	 */
	@Before("execution(public * com.example.controller.GirlController.findGirlByid(..))")
	public void logForFunction() {
		System.out.println("0000000");
	}
	
	/**
	 * 日志打印
	 */
	@Before("execution(public * com.example.controller.GirlController.*(..))")
	public void doBefore(JoinPoint joinpoint) {//joinpiont是为了获取类里面方法
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		//url
		logger.info("url={}",request.getRequestURL());
		//method
		logger.info("method={}",request.getMethod());
		//ip
		logger.info("ip={}",request.getRemoteAddr());
		//class method
		logger.info("class method={}",joinpoint.getSignature().getDeclaringTypeName()+"===="+joinpoint.getSignature().getName());
		//class method param
		logger.info("class method param={}",joinpoint.getArgs());
		logger.info("11111111111");
	}
	
	/**
	 * 日志打印
	 */
	@After("comLog()")
	public void doAfter() {
		logger.info("3333333333333");
		//System.out.println("3333333333");
	}
	
	/**
	 * 打印Response的结果
	 */
	@AfterReturning(returning="object",pointcut="comLog()")//告诉spring 入参是 object
	public void  doAfterReturning(Object object) {
		logger.info("response={}",object.toString());
	}
	
}
